import api from '../../../api/index';
import Toast from '@vant/weapp/toast/toast';
import Dialog from '@vant/weapp/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    evaluatesData: [],
    active: 0,
    titlelist:[
      '全部','好评','中评','差评'
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const {id} = options
    this.setData({
      id
    });
    this.evaluates(id)
  },
  onChange(event) {
    console.log(event.detail.index);
    
   if(event.detail.index !==0) {
      this.setData({
        evaluatesData: [],
      })
      let level = null
      if(event.detail.index == 1) {
        level = 'A'
      }
      if(event.detail.index == 2) {
        level = 'B'
      }
      if(event.detail.index == 3) {
        level = 'C'
      }
      this.evaluateslevel( this.data.id, level )
    } 
    if(event.detail.index ==0) {
      this.setData({
        evaluatesData: []
      })
      this.evaluates( this.data.id )
    }
    // wx.showToast({
    //   title: `切换到标签 ${event.detail.name}`,
    //   icon: 'none',
    // });
  },
  // 商品评价-等级
  async evaluateslevel(id,level) {
    const actions = {
			data: {
        goodsid: id,
        level
			}
		}
    try {
      const res = await api.post('/api/hxhealth/goods/evaluates',actions);
      console.log('=======商品评价==============');
      console.log(res);
      if(res && res.length > 0) {
        res.forEach(item => {
          const imgListStr = JSON.parse(item.images)
          const list = []
          item.stars = this.generateStars(item.stars)
          if(imgListStr.images && imgListStr.images.length > 0) {
            imgListStr.images.forEach(img => {
              list.push(img.file)
            })
          }
          item.showImg = list
        })
        console.log(res);
        this.setData({
          evaluatesData: res
        })
      console.log(this.data.evaluatesData);
      }
		} catch (e) {
			console.log('loadDesic->', e);
			return null;
		}
  },
  // 商品评价
  async evaluates(id) {
    const actions = {
			data: {
        goodsid: id,
        // level: 1
			}
		}
    try {
      const res = await api.post('/api/hxhealth/goods/evaluates',actions);
      console.log('=======商品评价==============');
      console.log(res);
      if(res && res.length > 0) {
        res.forEach(item => {
          const imgListStr = JSON.parse(item.images)
          const list = []
          item.stars = this.generateStars(item.stars)
          if(imgListStr.images && imgListStr.images.length > 0) {
            imgListStr.images.forEach(img => {
              list.push(img.file)
            })
          }
          item.showImg = list
        })
        console.log(res);
        this.setData({
          evaluatesData: res
        })
      console.log(this.data.evaluatesData);
      }
		} catch (e) {
			console.log('loadDesic->', e);
			return null;
		}
  },
  generateStars: function (count) {
    const starImageUrl = "http://lk-webfont.oss-cn-beijing.aliyuncs.com/redstart/shop/start.png";
    const starImages = [];
    for (let i = 0; i < count; i++) {
      starImages.push(starImageUrl);
    }
    return starImages;
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})